#include "musicslider.h"
#include "ui_musicslider.h"

MusicSlider::MusicSlider(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MusicSlider)
{
    ui->setupUi(this);

    _currentPosition = 0;
    _maxWidth = width();
    moveSlide();
}

MusicSlider::~MusicSlider()
{
    delete ui;
}

//移动进度条
void MusicSlider::moveSlide()
{
    if(_currentPosition < 0){
        _currentPosition = 0;
    }
    if(_currentPosition > _maxWidth){
        _currentPosition = _maxWidth;
    }
    // 根据当前进度设置外部滑动条的位置
    ui->outLine->setMaximumWidth(_currentPosition);
    ui->outLine->setGeometry(0,8, _currentPosition, 4);

}
//进度条同步当前播放时间
void MusicSlider::syncSlide(float value)
{
    _currentPosition = (qint64)_maxWidth * value;
    moveSlide();
}

void MusicSlider::mousePressEvent(QMouseEvent *event)
{
    _currentPosition = event->pos().x();
    moveSlide();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    // QRect rect = QRect(0,0,width(), height());
    // QPoint pos = event->pos();
    // if(!rect.contains(pos)){
    //     return;
    // }

    if(event->buttons() == Qt::LeftButton){
        _currentPosition = event->pos().x();
        moveSlide();
    }
}

void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
{
    _currentPosition = event->pos().x();
    moveSlide();

    emit setMusicSliderPostion((float)_currentPosition / (float)_maxWidth);
}
